package com.badlogic.androidgames;

import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
/**
 * @deprecated:播放背景音效
 * @author frank
 * @date 2015年11月22日
 */
public class SoundPoolTest extends Activity implements OnTouchListener {

	SoundPool soundPool;
	int explosionId = -1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TextView textView = new TextView(this);
		textView.setOnTouchListener(this);
		setContentView(textView);
		
		setVolumeControlStream(AudioManager.STREAM_MUSIC);
		//最多能播放20个音效，第二参数音频流
		soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
		
		try {
			AssetManager assetManager = getAssets();
			AssetFileDescriptor descriptor = assetManager.openFd("Cricket.ogg");
			explosionId = soundPool.load(descriptor, 1);	//第二个参数指定音效的优先级
		} catch (IOException e) {
			textView.setText("Couldn't load sound effect from asset, " + e.getMessage());
		}
	}
	
	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_UP){
			if(explosionId != -1){
				//第二、三个参数用于指定左右通道的音量0~1
				//第四个参数用于指定音效的优先级
				//第五个参数播放速率，>1快<1慢
				soundPool.play(explosionId, 1, 1, 0, 0, 1);
				//soundPool.unload(explosionId) 关闭释放内存
			}
		}
		return true;
	}
}
